package uk.co.jonlt;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.*;
import android.text.*;
import android.util.Log;

public class EasterCalc extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
	TextView year_value = null;
	SeekBar year_slider = null;
	String current_year = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        year_value = 
        	(TextView)findViewById(R.id.TextView01);
        
        year_value.setTextSize(20);
        
        year_value.setText(getCurrentYear());
        //text_filtered.setText(getCurrentYear());
        
        year_slider = (SeekBar)findViewById(R.id.SeekBar01);
        
        year_slider.setOnSeekBarChangeListener(
        		new SeekBar.OnSeekBarChangeListener() {
					
					@Override
					public void onStopTrackingTouch(SeekBar arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onStartTrackingTouch(SeekBar arg0) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
						// TODO Auto-generated method stub
						
						Integer value = progress + 1900;
						String textToSet = value.toString();
						
						current_year = textToSet;
						
						((TextView)findViewById(R.id.TextView01)).setText(textToSet); //progress + 1900);
						
						DisplayDate(current_year);
					}
				});

      
        
        current_year = getCurrentYear();
        
        year_slider.setProgress(Integer.parseInt(getCurrentYear())-1900);
        
        //text_filtered.
        
        ImageButton buttonUp = (ImageButton)findViewById(R.id.ImageButtonUp);
        buttonUp.setOnClickListener(this);
        
        ImageButton buttonDown = (ImageButton)findViewById(R.id.ImageButtonDown);
        buttonDown.setOnClickListener(this);
        
        //text_filtered.setOnKeyListener(this);
    }
    
    public void onClick(View v)
    {
    		switch(v.getId())
    		{
    		case R.id.ImageButtonUp:
    			year_value.setText(incYear(current_year));
    			break;
    		case R.id.ImageButtonDown:
    			year_value.setText(decYear(current_year));
    			break;
    		}
    		current_year = year_value.getText().toString();
    		
    		year_slider.setProgress(Integer.parseInt(current_year)-1900);
    		
    		DisplayDate(current_year);
    		
    		
    }
    
    private void DisplayDate(String year)
    {
    	int iyear = Integer.parseInt(year);
    	Calendar cal = GetEasterSunday(iyear);
    	String[] monthName = {"January", "February","March", "April", "May", "June", "July",
    	"August", "September", "October", "November","December"};
    	String dtVal = "Sunday," + cal.get(Calendar.DAY_OF_MONTH) + " " + monthName[cal.get(Calendar.MONTH)-1]; // + cal.get(Calendar.);
    	TextView result = (TextView)findViewById(R.id.TextViewResult);
    	result.setText(dtVal);
    }
    
    private String getCurrentYear()
    {
    	final Calendar c = Calendar.getInstance();
    	return Integer.toString(c.get(Calendar.YEAR));
    }
    
    public String incYear(String Year)
    {
    	int year = Integer.parseInt(Year);
    	if (year < 2099)
    	{
    		year++;
    	}
    	return Integer.toString(year);
    }
    
    private String decYear(String Year)
    {
    	int year = Integer.parseInt(Year);
    	if (year > 1900 )
    	year--;
    	return Integer.toString(year);
    }
    
    private boolean validDate(String year)
    {
    	int iyear = Integer.parseInt(year);
    	if ((iyear < 1900) && (iyear > 2099))
    		return false;
    	else
    		return true;
    }
    
    private static Calendar GetEasterSunday(int i)
    {
    	
    	Calendar c = Calendar.getInstance();
    	
    	int j = 225 - 11 * (i % 19);
        int k = (j - 21) % 30 + 21;
        int l = (i + i / 4 + k + 1) % 7;
        int i1 = (k + 7) - l;
        if(i1 < 32)
        {
                //EasterSundayBuffer.append("Sunday,\nMarch " + i1 + ", " + i);
                c.set(i, 3, i1);
        } 
        else
        {
                i1 -= 31;
                //EasterSundayBuffer.append("Sunday, April " + i1 + ", " + i);
                c.set(i, 4, i1);
        }
        
        return c;
    }    
}    
